import 'dart:math';
import 'package:flutter/cupertino.dart';
import '../api/api_service.dart';
import '../getit/ads/ads_service.dart';
import '../getit/service/service_locator.dart';
import 'ads_const.dart';
import '../usersp/user_sp.dart';
import '../../utils/k_log.dart';

class AdsUtils {
  static final AdsService _adService = getIt<AdsService>();

  //初始化广告
  static Future<bool> initAd() async {
    return _adService.initAd(AdsConst.appId);
  }

  //广告价格事件临时存储
  static Map<String, Map<String, String>> adEcpmEventData = {};
  static String posId = '';

  //广告监听
  static void setAdEvent() {
    _adService.onEventListener();
  }

  static Future<bool> requestIDFA() async {
    return _adService.requestIDFA();
  }

  //测试助手
  static void launchTestTools() {
    _adService.launchTestTools();
  }

  //开屏CSJ
  static Future<bool> showSplashAdCSJ({Function()? off}) async {
    var kpId = AdsConst.kpList[Random().nextInt(AdsConst.kpList.length)];
    //请求
    ApiService().appReport(
      actionType: AdsConst.actionType2,
      adId: "",
      placementId: kpId,
      adStyle: AdsConst.getAdStyle(kpId),
    );
    var showSplashAd = await _adService.showSplashAd(kpId);
    if (showSplashAd) {
      ///请求成功
      ApiService().appReport(
        actionType: AdsConst.actionType3,
        adId: "",
        placementId: kpId,
        adStyle: AdsConst.getAdStyle(kpId),
      );
    }
    return showSplashAd;
  }

  //开屏
  static Future<bool> showSplashAd({Function()? off}) async {
    if (!AdsConst.isOpenAds) {
      off?.call();
      return false;
    }

    var kpId = AdsConst.kpList[Random().nextInt(AdsConst.kpList.length)];

    //请求
    ApiService().appReport(
      actionType: AdsConst.actionType2,
      adId: "",
      placementId: kpId,
      adStyle: AdsConst.getAdStyle(kpId),
    );

    var showSplashAd = await _adService.showSplashAd(kpId);

    if (showSplashAd) {
      ///请求成功
      ApiService().appReport(
        actionType: AdsConst.actionType3,
        adId: "",
        placementId: kpId,
        adStyle: AdsConst.getAdStyle(kpId),
      );
    }

    return showSplashAd;
  }

  //激励视频
  static void showRewardVideoAd({Function()? off}) async {
    KLog.d('行为上报 onAdPresent');
    if (!AdsConst.isOpenAds) {
      off?.call();
      return;
    }

    var preloadID = AdsConst.jlspnList[Random().nextInt(AdsConst.jlspnList.length)];
    //请求
    ApiService().appReport(
      actionType: AdsConst.actionType2,
      adId: "",
      placementId: preloadID,
      adStyle: AdsConst.getAdStyle(preloadID),
    );

    await _adService.showRewardVideoAd(preloadID);
  }

  //插屏CSJ
  static void showInterstitialAdCSJ() async {
    //随机一个广告位ID
    var preloadID = AdsConst.cpList[Random().nextInt(AdsConst.cpList.length)];

    // //请求
    // ApiService().appReport(
    //   actionType: AdsConst.actionType2,
    //   adId: "",
    //   placementId: preloadID,
    //   adStyle: AdsConst.getAdStyle(preloadID),
    // );

    KLog.d(" AppConfigLog  showInterstitialAdCSJ $preloadID");
    var b = await _adService.showInterstitialAd(preloadID);

    if (b) {
      ///请求成功
      ApiService().appReport(
        actionType: AdsConst.actionType3,
        adId: "",
        placementId: preloadID,
        adStyle: AdsConst.getAdStyle(preloadID),
      );
    }
  }

  //插屏
  static void showInterstitialAd({Function()? off, bool isTime = true}) async {
    if (!AdsConst.isOpenAds) {
      off?.call();
      return;
    }

    //3秒内
    var s = await UserSp.instance.getShowAdTime();
    var millisecondsSinceEpoch = DateTime.now().millisecondsSinceEpoch;
    if ((millisecondsSinceEpoch - s) <= 3000 && isTime) {
      return;
    }

    var preloadID = AdsConst.cpList[Random().nextInt(AdsConst.cpList.length)];
    //请求
    ApiService().appReport(
      actionType: AdsConst.actionType2,
      adId: "",
      placementId: preloadID,
      adStyle: AdsConst.getAdStyle(preloadID),
    );

    await _adService.showInterstitialAd(preloadID);

    UserSp.instance.putShowAdTime(DateTime.now().millisecondsSinceEpoch);

  }

  //信息流
  static Future<Widget> requestFeedAdList({
    double width = 375,
    double height = 400,
    int count = 1,
  }) async {
    if (!AdsConst.isOpenAds) {
      return const SizedBox();
    }

    String posId = AdsConst.xxlList[Random().nextInt(AdsConst.xxlList.length)];

    // //请求
    ApiService().appReport(
      actionType: AdsConst.actionType2,
      adId: "",
      placementId: posId,
      adStyle: AdsConst.getAdStyle(posId),
    );

    Widget feedAdWidget = await _adService.loadFeedAd(
      posId,
      width: width,
      height: height,
      count: count,
      show: AdsConst.isOpenAds,
    );

    //请求
    ApiService().appReport(
      actionType: AdsConst.actionType3,
      adId: "",
      placementId: posId,
      adStyle: AdsConst.getAdStyle(posId),
    );
    return feedAdWidget;
  }

  static Future<Widget> requestFeedAdListCSJ({
    double width = 375,
    double height = 400,
    int count = 1,
  }) async {

    String posId = AdsConst.xxlList[Random().nextInt(AdsConst.xxlList.length)];

    // //请求
    // ApiService().appReport(
    //   actionType: AdsConst.actionType2,
    //   adId: "",
    //   placementId: posId,
    //   adStyle: AdsConst.getAdStyle(posId),
    // );

    Widget feedAdWidget = await _adService.loadFeedAd(
      posId,
      width: width,
      height: height,
      count: count,
      show: true,
    );

    //请求
    ApiService().appReport(
      actionType: AdsConst.actionType3,
      adId: "",
      placementId: posId,
      adStyle: AdsConst.getAdStyle(posId),
    );

    return feedAdWidget;
  }

  //预加载插屏和激励视频
  static void preload(){
    //激励视频
    _adService.preloadRewardPosids(AdsConst.jlspnList);
    //插屏
    _adService.preloadInsertPosids(AdsConst.cpList);
  }



}
